/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import client.Client;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ahmed
 */
public class ServerMonitor {

    public ServerMonitor() {
    }

    public void generateKeysForUsers(int n, int keyLength) {
        for (int i = 0; i < n; i++) {
            
        }
    }

    public void simulateDifferentKeySizes(int users, int messageLength, int numberofMessages) {
        ArrayList<MonitorReadings> symResults = new ArrayList<MonitorReadings>();
        ArrayList<MonitorReadings> asymResults = new ArrayList<MonitorReadings>();
        int[] keyLengths = {64,96,128,192};
        for (int len : keyLengths) {
            generateKeysForUsers(users, len);
            Client[] symClients = new Client[users];
            for (int i = 0; i < users; i++) {
                symClients[i] = new Client("user" + i, "12345");
            }
            Client[] asymClients = new Client[users];
            for (int i = 0; i < users; i++) {
                asymClients[i] = new Client("user" + i, "12345");
            }
            Server asymServer = new Server(Server.PUBLIC_PRIVATE, 5000);
            asymServer.enableMonitor(true);
            Server symServer = new Server(Server.PASSWORD, 5001);
            symServer.enableMonitor(true);
            for (int i = 0; i < users; i++) {
                symClients[i].initializeClient("127.0.0.1", 5000);
            }
            for (int i = 0; i < users; i++) {
                asymClients[i].initializeClient("127.0.0.1", 5001);
            }
            for (int i = 0; i < numberofMessages; i++) {
                for (int j = 0; j < users; j++) {
                    String msg = generateMsg(messageLength);
                    symClients[j].sendNewMessage(msg);
                    asymClients[j].sendNewMessage(msg);
                }
            }
            symResults.add(symServer.getMonitorReadings());
            asymResults.add(asymServer.getMonitorReadings());
        }
        saveResultToFile(symResults, "symResults.txt");
        saveResultToFile(asymResults, "asymResults.txt");
    }

    private String generateMsg(int messageLength) {
        StringBuilder buf = new StringBuilder();
        Random rand = new Random();
        for (int i = 0; i < messageLength; i++) {
            buf.append((char) ('a' + rand.nextInt(26)));
        }
        return buf.toString();
    }

    private void saveResultToFile(ArrayList<MonitorReadings> results, String string) {
        PrintWriter out = null;
        try {
            Iterator<MonitorReadings> it = results.iterator();
            out = new PrintWriter(new FileWriter(new File(string)));
            out.write("#encPeriod encNumber decPeriod decNumber\n");
            while (it.hasNext())
            {
                MonitorReadings temp = it.next();
                out.write(temp.getEncPeriod() + " " + temp.getEncNumber() + " " 
                        + temp.getDecPeriod() + " " + temp.getDecNumber() + "\n");
            }
        } catch (IOException ex) {
            Logger.getLogger(ServerMonitor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }
    }
}
